﻿// Nail.WindowsContextMenu Library
// Nail framework
//
// Copyright © Frank Nagl, 2008-2011
// admin@franknagl.de
// www.franknagl.de
//
namespace ContextMenu
{
    using System;
    using System.Windows.Forms;
    using Microsoft.Win32;

    /// <summary>
    /// Provides static functions to add and remove Windows context menu entries for
    /// special file formats.
    /// </summary>
    public static class WindowsContextMenu
    {
        /// <summary>Adds the Picturez registry entries.</summary>
        /// <param name="programExe">The Picturez executable file.</param>
        public static void AddPicturezEntries(string programExe)
        {
            RegistryKey regmenu;
            try
            {
                // CONVERT WITH PICTUREZ
                regmenu = Registry.ClassesRoot.CreateSubKey(
                    "*\\shell\\Convert with Picturez\\Command");
                if (regmenu != null)
                {
                    regmenu.SetValue("", "\"" + programExe + "\" \"%1\"");
                    regmenu.Close();
                }

                // EDIT WITH PICTUREZ
                regmenu = Registry.ClassesRoot.CreateSubKey(
                    "*\\shell\\Edit with Picturez\\Command");
                if (regmenu != null)
                {
                    regmenu.SetValue("", "\"" + programExe + "\" \"%1\" \"edit.frank\"");
                    regmenu.Close();
                }

                // CONVERT FOLDER WITH PICTUREZ
                regmenu = Registry.ClassesRoot.CreateSubKey(
                    "Folder" + "\\shell\\Convert with Picturez\\Command");
                if (regmenu != null)
                {
                    regmenu.SetValue("", "\"" + programExe + "\" \"%1\" \"dir.frank\"");
                    // regmenu.Close();
                    regmenu.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        /// <summary>Adds the SBIP registry entry.</summary>
        /// <param name="programExe">The SBIP executable file.</param>
        public static void AddSBIPEntries(string programExe)
        {
            RegistryKey regmenu;
            try
            {
                // open (or create) and set value of extension entry in HKEY_CLASSES_ROOT
                regmenu = Registry.ClassesRoot.CreateSubKey("*\\shell\\Open with SBIP\\Command");
                if (regmenu != null)
                {
                    regmenu.SetValue("", "\"" + programExe + "\" \"%1\" \"sbip.frank\"");
                    regmenu.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        /// <summary>Adds the Serotonin registry entry.</summary>
        /// <param name="programExe">The Serotonin executable file.</param>
        public static void AddSerotoninEntries(string programExe)
        {
            RegistryKey regmenu;
            try
            {
                // open (or create) and set value of extension entry in HKEY_CLASSES_ROOT
                regmenu = Registry.ClassesRoot.CreateSubKey("*\\shell\\Serotonin\\Command");
                if (regmenu != null)
                {
                    regmenu.SetValue("", "\"" + programExe + "\" \"%1\" \"serotonin.frank\"");
                    regmenu.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        /// <summary>Removes the Picturez registry entries.</summary>
        public static void RemovePicturezEntries()
        {
            try
            {
                Registry.ClassesRoot.DeleteSubKey(
                    "*\\shell\\Convert with Picturez\\Command", false);
                Registry.ClassesRoot.DeleteSubKey(
                    "*\\shell\\Convert with Picturez", false);
                Registry.ClassesRoot.DeleteSubKey(
                    "*\\shell\\Edit with Picturez\\Command", false);
                Registry.ClassesRoot.DeleteSubKey(
                    "*\\shell\\Edit with Picturez", false);
                Registry.ClassesRoot.DeleteSubKey(
                    "Folder\\shell\\Convert with Picturez\\Command", false);
                Registry.ClassesRoot.DeleteSubKey(
                    "Folder\\shell\\Convert with Picturez", false);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        /// <summary>Removes the SBIP registry entries.</summary>
        public static void RemoveSBIPEntries()
        {
            try
            {
                Registry.ClassesRoot.DeleteSubKey(
                    "*\\shell\\Open with SBIP\\Command", false);
                Registry.ClassesRoot.DeleteSubKey(
                    "*\\shell\\Open with SBIP", false);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        /// <summary>Removes the Serotonin registry entries.</summary>
        public static void RemoveSerotoninEntries()
        {
            try
            {
                Registry.ClassesRoot.DeleteSubKey(
                    "*\\shell\\Serotonin\\Command", false);
                Registry.ClassesRoot.DeleteSubKey(
                    "*\\shell\\Serotonin", false);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        /// <summary>
        /// Removes the Windows context menu entries of the passed file formats.
        /// </summary>
        /// <param name="formats">The list of file formats,
        /// where the context menu entries will be removed.</param>
        /// <param name="menuOption">The new menu option string for the registry.</param>
        public static void RemoveOldPicturezEntries(string[] formats, string menuOption)
        {
            RegistryKey reg;
            string name = "nil";
            foreach (string f in formats) 
            {
                try
                {
                    reg = Registry.ClassesRoot.OpenSubKey(f, false);
                    //If format already exists, get its value name, otherwise return.
                    if (reg != null)
                        name = (String)reg.GetValue("");
                    else
                        return;
                    reg.Close();

                    //Delete Windows context menu entry
                    reg = Registry.ClassesRoot.OpenSubKey(name + menuOption +
                                                          "\\Command");
                    if (reg != null)
                    {
                        Registry.ClassesRoot.DeleteSubKey(name + menuOption +
                                                          "\\Command");
                        Registry.ClassesRoot.DeleteSubKey(name + menuOption);
                    }

                    // old folder entry
                    Registry.ClassesRoot.DeleteSubKey(
                        "Folder\\shell\\ConvertWithPicturez\\Command", false);
                    Registry.ClassesRoot.DeleteSubKey(
                        "Folder\\shell\\ConvertWithPicturez", false);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(name + ":\n\n" + ex);
                }
            }
        }
    }
}
